using B.S.XZYData.Api.Read.Application.Command.Process;
using B.S.XZYData.Domain.ZSHH;
using B.S.XZYData.ErrorCode;
using B.S.XZYData.Infrastructure.Interface;
using MediatR;
using Microsoft.Extensions.Logging;
using SqlSugar;

namespace B.S.XZYData.Api.Read.Application.Handler.Process
{
    /// <summary>
    /// 获取所有工序名称处理器
    /// </summary>
    public class GetAllProcessNamesHandler : IRequestHandler<GetAllProcessNamesCommand, ApiResult<List<ProcessNameDto>>>
    {
        private readonly IBaseRepository<Domain.ZSHH.Process> _processRepo;
        private readonly ILogger<GetAllProcessNamesHandler> _logger;

        public GetAllProcessNamesHandler(IBaseRepository<Domain.ZSHH.Process> processRepo,
            ILogger<GetAllProcessNamesHandler> logger)
        {
            _processRepo = processRepo;
            _logger = logger;
        }

        public async Task<ApiResult<List<ProcessNameDto>>> Handle(GetAllProcessNamesCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<List<ProcessNameDto>>();
            try
            {
                var list = await _processRepo.GetAll()
                    .Where(x => !x.IsDeleted)
                    .OrderBy(x => x.Id)
                    .Select(x => new ProcessNameDto
                    {
                        Id = x.Id,
                        ProcessName = x.ProcessName
                    })
                    .ToListAsync();

                res.Data = list;
                res.Code = ApiEnum.Success;
                res.Msg = "成功";
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取工序名称列表异常");
                res.Code = ApiEnum.Error;
                res.Msg = ex.Message;
            }

            return res;
        }
    }
} 